<?php
/**
 * Created by PhpStorm.
 * User: xlosy
 * Date: 2018/4/14
 * Time: 10:33
 */

namespace app\index\controller;

use app\index\model\User;
use think\Controller;

/**
 * 权限验证类
 * Class Verification
 * @package app\manager\controller
 */
class Commonverification extends Controller
{
    /**
     * 用户登录
     */
    public function loginInput(){

        return $this->fetch();
    }

    /**
     * 用户登录验证
     */
    public function login(){
        $username = input("account_l");
        $password = input("password_l");
        $user = new User();
        $user->whereOr('userEmail',$username);
        $user->whereOr('userName',$username);
        $user_db =  $user ->find();
        if($user_db!=null){
            if(md5($password) == ($user_db->userPasswd)){
                session('uid',$user_db);
                return json(['code'=>'1']);
            }
        }
        return json(['code'=>'0']);
    }

    /**
     * 用户登出
     */
    public function login_out(){
        session('uid',null);
        return json(['code'=>'1']);
    }

    /**
     * 用户注册输入
     */
    public function register_input(){
        return $this->fetch();
    }

    /**
     * 用户注册
     */
    public function register(){
        $user = new User();
        $rtn_ob = $user->where(["userEmail"=>input("userEmail")])->find();
        if($rtn_ob==null){
            $rtn_obh = $user->where(["userName"=>input("userName")])->find();
            if($rtn_obh==null){
                $rtn = $user->addUser();
                if($rtn){
                    $rtn_obj = ['code'=>'1'];

                }else{
                    $rtn_obj['code'] = '0';
                }
            }else{
                $rtn_obj['code'] = '0';
                $rtn_obj['error'] = '该用户名已存在！';
            }
        }else{
            $rtn_obj['code'] = '0';
            $rtn_obj['error'] = '该邮箱已经注册！';
        }
        return json($rtn_obj);
    }
}